﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqToLfsWorld.Extensions
{
	/// <summary>
	/// Contains various DateTime extension methods.
	/// </summary>
	public static class DateTimeExtensions
	{
		/// <summary>
		/// Gets the epoch date value
		/// </summary>
		public static DateTime Epoch
		{
			get { return DateTime.Parse("1/1/1970").ToLocalTime(); }
		}

		/// <summary>
		/// Converts the extended DateTime value to its equivalent unix timestamp value.
		/// </summary>
		/// <param name="dt">The extended datetime value.</param>
		public static int ToUnixTimestamp(this DateTime dt)
		{
			return (int)(dt.ToLocalTime() - Epoch).TotalSeconds;
		}

		/// <summary>
		/// Converts <paramref name="unixTimeStamp"/> to its unix timestamp equivalent.
		/// </summary> 
		/// <param name="unixTimestamp">The unix timestamp.</param>
		public static DateTime FromUnixTimestamp(int unixTimestamp)
		{
			TimeSpan ts = TimeSpan.FromSeconds(unixTimestamp);

			return (Epoch + ts).ToLocalTime();
		}

	}
}
